using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Configuration;
using Admaco.Data;
using LumenWorks.Framework.IO.Csv;

namespace Admaco.Presentation
{
    public partial class ImportarDatos : Form
    {
        public ImportarDatos()
        {
            InitializeComponent();
        }

        private void ImportarDatos_Load(object sender, EventArgs e)
        {

        }

        private void botonCancelar_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void examinarDatosButton_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    int fieldCount=0;
                    int recordCount=0;

                    using (CsvReader csv = new CsvReader(new StreamReader(openFileDialog1.FileName), false, ';'))
                    {
                        fieldCount = csv.FieldCount;

                        while (csv.ReadNextRecord()) {
                            recordCount++;
                        } 
                    }

                    camposText.Text = fieldCount.ToString();
                    registrosText.Text = recordCount.ToString();

                    datosText.Text = openFileDialog1.FileName;
                    ToolTip toolTip = new ToolTip();
                    toolTip.SetToolTip(datosText, datosText.Text);                                                            

                }
                catch (Exception ex) 
                {
                    MessageBox.Show("Error al leer el fichero de datos: " + ex, "Error de lectura", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                
            }
        }

        /// <summary>
        /// Cargo el fichero en el sistema
        /// </summary>                
        private void botonCargar_Click(object sender, EventArgs e)
        {
            AppSettingsReader appReader = new AppSettingsReader();
            String stringType = "";
            String dataDirectory = (String)appReader.GetValue("dataDirectory", stringType.GetType());

            String path = dataDirectory + @"\";
            String ficheroClases = "";
            String ficheroDatos = "";
            
            try
            {
                // Copio los ficheros al directorio de datos
                ficheroClases = Path.GetFileName(clasesText.Text);
                ficheroDatos = Path.GetFileName(datosText.Text);
                
                File.Copy(clasesText.Text, path + ficheroClases);
                File.Copy(datosText.Text, path + ficheroDatos);

                // Almaceno los ficheros en la base de datos
                Fichero fichero = new Fichero();
                fichero.Usuario = Principal.UsuarioConectado;
                fichero.Titulo = tituloText.Text;
                fichero.Comentario = comentariosText.Text;
                fichero.Fecha = DateTime.Now;
                fichero.FicheroClases = path + ficheroClases;
                fichero.FicheroDatos = path + ficheroDatos;
                fichero.save();                
            }
            catch (Exception ex)
            {
                if (File.Exists(path + ficheroClases)) File.Delete(path + ficheroClases);
                if (File.Exists(path + ficheroDatos)) File.Delete(path + ficheroDatos);

                MessageBox.Show("Ha ocurrido un error al cargar el fichero en la base de datos: " + ex, "Error de carga",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
            Close();
        }

        private void examinarClasesButton_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    clasesText.Text = openFileDialog1.FileName;
                    ToolTip toolTip = new ToolTip();
                    toolTip.SetToolTip(clasesText, clasesText.Text);                                        
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error al leer el fichero de clases: " + ex, "Error de lectura", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }

            }

        }

        private Boolean validar()
        {
            if ((datosText.Text != "") && (clasesText.Text != "") && (tituloText.Text != "") && (comentariosText.Text != ""))
                return true;
            else
                return false;
        }

        private void datosText_TextChanged(object sender, EventArgs e)
        {
            if (validar())
                botonCargar.Enabled = true;
            else
                botonCargar.Enabled = false;
        }

        private void clasesText_TextChanged(object sender, EventArgs e)
        {
            if (validar())
                 botonCargar.Enabled = true;
            else
                 botonCargar.Enabled = false;
        }

        private void tituloText_TextChanged(object sender, EventArgs e)
        {
            if (validar())
                botonCargar.Enabled = true;
            else
                botonCargar.Enabled = false;
        }

        private void comentariosText_TextChanged(object sender, EventArgs e)
        {
            if (validar())
                botonCargar.Enabled = true;
            else
                botonCargar.Enabled = false;
        }

        private void camposText_TextChanged(object sender, EventArgs e)
        {

        }

        private void ImportarDatos_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            Help.ShowHelp(this, "AdmacoHelp.chm", HelpNavigator.TopicId, "4");
        }

        private void ImportarDatos_HelpRequested(object sender, HelpEventArgs hlpevent)
        {
            Help.ShowHelp(this, "AdmacoHelp.chm", HelpNavigator.TopicId, "4");
        }

    }
}